﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Win32;
using WWLauncher.Constant;
using WWLauncher.ViewModel;

namespace WWLauncher.Util
{
    /// <summary>
    /// 做完，发现没有什么用，被Closing事件代替了。-_-!!
    /// </summary>
    public class ConfigUtil
    {
        private static readonly object _MyLock = new object();
        private readonly System.Windows.Forms.Timer _timer;
        public Window ParentWindow { get; set; }
        public ConfigUtil(Window window)
        {
            ParentWindow = window;

            _timer = new System.Windows.Forms.Timer
            {
                Interval = 10 * 1000
            };
            _timer.Tick += PollingWorking;
        }
        /// <summary>
        /// 轮询工作
        /// </summary>
        /// <param name="o"></param>
        /// <param name="e"></param>
        private void PollingWorking(object o, EventArgs e)
        {
            lock (_MyLock)
            {
            }
        }
        public void Start()
        {
            _timer.Start();
        }

        public void Stop()
        {
            PollingWorking(null, null);
            _timer.Stop();
        }

        /// <summary>
        /// 保存app 数据
        /// </summary>
        public static void SaveAppData()
        {
            var data = MainViewModel.Instance();
            SaveAppData(data, Constants.DataFilePath);
        }
        /// <summary>
        /// 保存app 数据
        /// </summary>
        /// <param name="appData"></param>
        /// <param name="filePath"></param>
        public static void SaveAppData(object appData, string filePath)
        {
            lock (_MyLock)
            {
                if (!Directory.Exists(filePath.Substring(0, filePath.LastIndexOf("\\"))))
                {
                    Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf("\\")));
                }

                SerializableHelper.Serialize(appData, filePath, DataFormart.Json);
            }
        }
        public static MainViewModel RefreshAppData(FileInfo fi)
        {
            var data = SerializableHelper.DeSerialize<MainViewModel>(fi, DataFormart.Json);
            return data;
        }

        public static void SavePassword(string password)
        {
            using StreamWriter sw = new StreamWriter(Constants.PWFilePath);
            sw.Write(password);
        }
        public static void BakAppData(Window window)
        {
            SaveFileDialog sfd = new SaveFileDialog
            {
                Title = "备份文件",
                InitialDirectory = Constants.AppDir + "data",
                Filter = "bak文件(*.bak)|*.bak",
                FileName = "Data-" + DateTime.Now.ToString("yyyyMMdd") + ".bak",
            };
            if (sfd.ShowDialog(window) == true)
            {
                SerializableHelper.Serialize(MainViewModel.Instance(), sfd.FileName, DataFormart.Json);
            }
        }
    }
}
